/**
 * 
 */
package com.vision.core.sv.serv.rpc;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.vision.core.cm.data.Domain;
import com.vision.core.cm.db.data.Permission;
import com.vision.core.cm.exception.RpcException;
import com.vision.core.cm.serv.rpc.PermissionService;
import com.vision.core.sv.db.mgr.IDataManager;
import com.vision.core.sv.serv.rpc.AbstractServiceImpl;

/**
 * @author Mark
 *
 */
@Service("permissions")
public class PermissionServiceImpl extends AbstractServiceImpl<Permission> implements PermissionService {

	private static final long serialVersionUID = 1L;
	
	public PermissionServiceImpl() {
		initGilead();
	}

	@Autowired
	@Qualifier("permissionManager")
	@Override
	public void setDataManager(IDataManager<Permission> dataManager) {
		super.setDataManager(dataManager);
	}
	
	@Override
	public List<Permission> getAll() throws RpcException {
		List<Permission> permissions = super.getAll();
		
		Collections.sort(permissions, new Comparator<Permission>() {
			@Override
			public int compare(Permission p1, Permission p2) {
				int diff = p1.getApplicationId().compareTo(p2.getApplicationId());
				if (diff != 0) return diff;
				
				diff = p1.getDomain().compareTo(p2.getDomain());
				if (diff != 0) return diff;
				
				return p1.getName().compareTo(p2.getName());
			}
		});
		
		return permissions;
	}
	
	/* (non-Javadoc)
	 * @see com.vision.core.sv.serv.rpc.AbstractServiceImpl#getDomain()
	 */
	@Override
	protected Domain getDomain() {
		return Domain.PERMISSION;
	}

}
